package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.system.entity.SysUserRole;
import com.mars.module.system.mapper.SysUserMapper;
import com.mars.module.system.mapper.SysUserRoleMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysFreebackRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysFreebackMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysFreeback;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysFreebackService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 患者反馈业务层处理
 *
 * @author mars
 * @date 2024-04-11
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysFreebackServiceImpl implements ISysFreebackService {

    private final SysFreebackMapper baseMapper;
    private final SysUserMapper userMapper;
    private final SysUserRoleMapper roleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysFreeback add(SysFreebackRequest request) {
        SysFreeback entity = SysFreeback.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setUserId(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysFreebackRequest request) {
        SysFreeback entity = SysFreeback.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysFreeback getById(Long id) {
        SysFreeback freeback = baseMapper.selectById(id);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        Long l = roleMapper.selectCount(new QueryWrapper<SysUserRole>().eq("user_id", userId).eq("role_id", "1227987566360264704"));
        if (l > 0) {
            freeback.setFlag("1");
        } else {
            freeback.setFlag("0");
        }
        freeback.setUserName(userMapper.selectById(freeback.getUserId()).getUserName());
        return freeback;
    }

    @Override
    public PageInfo<SysFreeback> pageList(SysFreebackRequest request) {
        Page<SysFreeback> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysFreeback> query = this.buildWrapper(request);
        IPage<SysFreeback> pageRecord = baseMapper.selectPage(page, query);
        List<SysFreeback> records = pageRecord.getRecords();
        List<SysFreeback> recordsNew = new ArrayList<>();
        for (SysFreeback freeback : records) {
            freeback.setUserName(userMapper.selectById(freeback.getUserId()).getUserName());
            Long userId = ContextUserInfoThreadHolder.get().getId();
            Long l = roleMapper.selectCount(new QueryWrapper<SysUserRole>().eq("user_id", userId).eq("role_id", "1227987566360264704"));
            if (l > 0) {
                freeback.setFlag("1");
            } else {
                freeback.setFlag("0");
            }
            recordsNew.add(freeback);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysFreeback> list(SysFreebackRequest request) {
        LambdaQueryWrapper<SysFreeback> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysFreeback> buildWrapper(SysFreebackRequest param) {
        LambdaQueryWrapper<SysFreeback> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(SysFreeback::getContent, param.getContent());
        }
        if (StringUtils.isNotBlank(param.getAnswer())) {
            query.like(SysFreeback::getAnswer, param.getAnswer());
        }
        return query;
    }

}
